const app = getApp()
let host = app.globalData.host;
Page({
 
  /**
   * 页面的初始数据
   */
  data: {
  },
 
  //表单提交
  formSubmit(e){

    this.setData({
      information: e.detail.value,
      modalHidden:false
    });

    let {cropId,recordMain,recordDate,recordImg} = e.detail.value
    let newDate = new Date(recordDate)
    wx.request({
      url: host+'/addNewCropRecord',
      data: {
        cropId: cropId,
        recordDate: newDate,
        recordMain: recordMain,
        recordImg: recordImg
      },
      method: "POST",
      header: {
        'content-type': 'application/x-www-form-urlencoded' // 默认值
      },success (res) {
      console.log(res.data)
      },
      fail (error){
        console.log("失败")
      }
    })
    wx.redirectTo({
      url: '../cropMain/cropMain?cropId=' + cropId,
    })

  },
  bindDateChange: function (e) {
    this.setData({
      date: e.detail.value
    })
  },
  unploadImg(e) {
    let that = this
    wx.chooseImage({
      count: 1,
      sizeType: ['original', 'compressed'],
      sourceType: ['album', 'camera'],
      success(res) {
        that.setData({
            'uploadImg': res.tempFilePaths
        })

        wx.uploadFile({
          url: "/uploadImg",
          filePath: res.tempFilePaths[0],
          name: 'file',
          success: (res) =>{
            let uploadImg = res.data
            console.log(uploadImg)
            that.setData({
              recordImg: uploadImg
            })
           },
           fail(err) {
             console.log(err)
          }
         })
      },
      fail(error) {
        console.log(error)
      }
      })
  },

  onLoad: function (options) {

    let that = this
    let cropId = options.cropId
    wx.request({
      url: host+'/showGrowthNotes?cropId='+cropId,
      method: "GET",
      header: {
        'content-type': 'application/x-www-form-urlencoded' // 默认值
      },success (res) {
        that.setData({
          cropId: res.data.cropId,
          deviceSerial: res.data.cropDeviceSerial,
        })
      },
      fail (error){
        console.log("失败")
      }
    })
  },
  getSnap:function(e) {

    let that = this
    let deviceSerial = e.target.dataset.deviceserial
    wx.request({
      url: host + '/showCamera?cameraId=1',
      method: "GET",
      header: {
        'content-type': 'application/x-www-form-urlencoded' // 默认值
       },success (res) {
        let accessToken = res.data.cameraAccessToken
        wx.request({
          url: 'https://open.ys7.com/api/lapp/device/info',
          method: "POST",
          data: {
            accessToken: accessToken,
            deviceSerial: deviceSerial
          },
          header: {
            'content-type': 'application/x-www-form-urlencoded' // 默认值
           },success (res) {
             if (res.data.data.status == 1) {
               wx.request({
                 url: 'https://open.ys7.com/api/lapp/device/capture',
                 method: "POST",
                 data: {
                  accessToken: accessToken,
                  deviceSerial: deviceSerial,
                  channelNo: 1
                 },
                 header: {
                  'content-type': 'application/x-www-form-urlencoded' // 默认值
                 },success (res) {
                   let imgUrl = res.data.data.picUrl
                   wx.request({
                    url: host+'/SnapImgToCloud',
                    method: "POST",
                    data: {
                      imgUrl: imgUrl
                    },
                    header: {
                      'content-type': 'application/x-www-form-urlencoded' // 默认值
                    },success (res) {
                      console.log(res)
                      that.setData({
                        uploadImg: res.data,
                        recordImg: res.data,
                      })
                    },
                    fail (error){
                      console.log("失败")
                    }
                   })
                 },fail (error){
                  console.log("失败")
               }
               })
             }
           },
        fail (error){
           console.log("失败")
        }
        })
       },
      fail (error){
       console.log("失败")
      }
    })
  }
})

